//
//  TSMessageListCell.m
//  chart
//
//  Created by xu on 15/8/12.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSMessageListCell.h"

#define MESSAGE_CELL_GAP 5.f
#define TIME_LABEL_WIDTH 160.f
#define SECTION_LABEL_WIDTH (currentDeviceWidth-TIME_LABEL_WIDTH-STATUS_LABEL_WIDTH-2*MESSAGE_CELL_GAP-2*MARGIN_LEFT)
#define STATUS_LABEL_WIDTH 50.f

@implementation TSMessageListCell{
    UILabel *timeAndTitleLabel;
    UILabel *sectionAndNameLabel;
    UILabel *statusLabel;
}

@synthesize isSendNotReceive;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self addTimeAndTitleLabel];
        [self addSectionAndNameLabel];
        [self addStatusLabel];
    }
    
    return self;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier isHeader:(BOOL)isHeader
{
    self = [self initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(isHeader){
        [self setHeadContent];
    }
    return self;
}

-(void)setHeadContent
{
    if (timeAndTitleLabel) {
        [timeAndTitleLabel setText:[NSString stringWithFormat:@"%@                  %@", @"时间", @"标题"]];
    }
    
    if (sectionAndNameLabel) {
        [sectionAndNameLabel setText:[NSString stringWithFormat:@"%@-%@", @"部门", @"用户名"]];
    }
    
    if (statusLabel) {
        [statusLabel setText:@"状态"];
    }
}

- (void)addTimeAndTitleLabel
{
    timeAndTitleLabel = [self createLabel:CGRectMake(MARGIN_LEFT, 0, TIME_LABEL_WIDTH, CGRectGetHeight(self.bounds)) TextAignment:NSTextAlignmentLeft];
    [self.contentView addSubview:timeAndTitleLabel];
}

- (void)addSectionAndNameLabel
{
    sectionAndNameLabel = [self createLabel:CGRectMake(CGRectGetMaxX(timeAndTitleLabel.frame)+MESSAGE_CELL_GAP, 0, SECTION_LABEL_WIDTH, CGRectGetHeight(self.bounds)) TextAignment:NSTextAlignmentCenter];
    [self.contentView addSubview:sectionAndNameLabel];
}

- (void)addStatusLabel
{
    statusLabel = [self createLabel:CGRectMake(CGRectGetMaxX(sectionAndNameLabel.frame)+MESSAGE_CELL_GAP, 0, STATUS_LABEL_WIDTH, CGRectGetHeight(self.bounds)) TextAignment:NSTextAlignmentRight];
    [self.contentView addSubview:statusLabel];
}

- (UILabel *)createLabel:(CGRect)frame TextAignment:(NSTextAlignment)alignment
{
    UILabel *label = [[UILabel alloc] initWithFrame:frame];
    [label setText:@""];
    label.textAlignment = alignment;
    [label setFont:FONT(12.f)];
    
    return label;
}

- (void)setModel:(TSReceiveMsgModel *)model
{
    if (timeAndTitleLabel) {
        [timeAndTitleLabel setText:[NSString stringWithFormat:@"%@     %@", [TSTools getDateNoYearByTime:model.time], model.title]];
    }
    
    if (sectionAndNameLabel) {
        [sectionAndNameLabel setText:[NSString stringWithFormat:@"%@-%@", model.sendSection, isSendNotReceive?model.send:model.receive]];
    }
    
    if (statusLabel) {
        [statusLabel setText:[TSTools getReceiveStatusTypeString:model.receiveStatus.integerValue]];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
